package com.waverider.promotion.biz.dao.ibatis;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
import com.waverider.promotion.biz.dao.PromotionDAO;
import com.waverider.promotion.domain.PromotionDO;
import com.waverider.promotion.model.PromotionQueryModel;

@SuppressWarnings("unchecked")
public class PromotionDAOImpl extends SqlMapClientDaoSupport implements
		PromotionDAO {
	@Override
	public Long insert(PromotionDO promotion) {
		try {
			return (Long) this.getSqlMapClient().insert("promotion.dao.insert",
					promotion);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public PromotionDO queryByGoodsId(Long goodsId) {
		try {
			return (PromotionDO) this.getSqlMapClient().queryForObject(
					"promotion.dao.queryByGoodsId", goodsId);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public PromotionDO queryById(Long id) {
		try {
			return (PromotionDO) this.getSqlMapClient().queryForObject(
					"promotion.dao.queryById", id);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public List<PromotionDO> query(PromotionQueryModel queryModel) {
		try {
			return (List<PromotionDO>) this.getSqlMapClient().queryForList(
					"promotion.dao.query", queryModel);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public Long count(PromotionQueryModel queryModel) {
		try {
			return (Long) this.getSqlMapClient().queryForObject(
					"promotion.dao.count", queryModel);
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public boolean update(PromotionDO promotion) {
		try {
			this.getSqlMapClient().startTransaction();
			int rows = this.getSqlMapClient().delete("promotion.dao.update",
					promotion);
			if (rows > 1) {
				throw new RuntimeException("[ERROR] Not possible!");
			}
			this.getSqlMapClient().commitTransaction();
			if (rows == 1) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			try {
				this.getSqlMapClient().endTransaction();
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
	}

	@Override
	public boolean delete(Long id) {
		try {
			this.getSqlMapClient().startTransaction();
			int rows = this.getSqlMapClient()
					.delete("promotion.dao.delete", id);
			if (rows > 1) {
				throw new RuntimeException("[ERROR] Not possible!");
			}
			this.getSqlMapClient().commitTransaction();
			if (rows == 1) {
				return true;
			} else {
				return false;
			}
		} catch (SQLException e) {
			throw new RuntimeException(e);
		} finally {
			try {
				this.getSqlMapClient().endTransaction();
			} catch (SQLException e) {
				throw new RuntimeException(e);
			}
		}
	}

	@Override
	public List<PromotionDO> getPromotionByShopId(Map<String, Long> params) {
		List<PromotionDO> promotionList = null;
		try {
			promotionList = (List<PromotionDO>) this.getSqlMapClient()
					.queryForList("promotion.dao.queryByShopId", params);
		} catch (SQLException e) {
			throw new RuntimeException(e.getMessage(), e);
		}
		return promotionList;
	}

	@Override
	public List<PromotionDO> queryExpirePromotionByShopId(Long shopId) {
		List<PromotionDO> promotionList = null;
		try {
			promotionList = (List<PromotionDO>) this.getSqlMapClient()
					.queryForList("promotion.dao.queryExpirePromotionByShopId",
							shopId);
		} catch (SQLException e) {
			throw new RuntimeException(e.getMessage(), e);
		}
		return promotionList;
	}

	@Override
	public int getValidatePromotionCountByShopId(Long shopId) {

		int total = 0;
		try {
			total = (Integer) this.getSqlMapClient().queryForObject(
					"promotion.dao.getValidatePromotionCountByShopId", shopId);
		} catch (SQLException e) {
			throw new RuntimeException(e.getMessage(), e);
		}
		return total;

	}
}